package org.charless.skeleton.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.charless.skeleton.UserManager;
import org.charless.skeleton.bo.User;
import org.charless.skeleton.dao.UserDao;
import org.springframework.beans.factory.annotation.Required;

/**
 * This service handle the Datt users management:
 *  - creation, user informations, ...
 * @author charless
 *
 */
@Service("UserManager")
public class UserManagerImpl implements UserManager {
	// Will be injected by Spring
	private UserDao userDao;

	 /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());
	
	/******************************************************************************/
    /** Working with users                                                        */
    /******************************************************************************/ 
    /**
     * Get the list of all datt users
	 * @return The list of all existing users
	 */
    @Override
	public List<User> getAllUsers() {
		return this.userDao.findAll();
	}
	
    /**
     * Return the user model object referenced by the given username
     * If no such a user already exists, create it
     * @param username The (unique) name of the user 
     * @return The user data model
     */
    @Override
	public User getUser(String username) {
    	User user = this.userDao.getByName(username);
    	if (user == null) {
    		user = new User();
    		user.setName(username);
    		this.userDao.saveOrUpdate(user);
    	}
    	return user;
    }
	
	/******************************************************************************/
    /** Setter/Getter                                                             */
    /******************************************************************************/ 
    public UserDao getUserDao() {
		return userDao;
	}
    
    @Required
    @Resource(name="UserDAO")
    public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
